package com.xwq.administrator.lljk;

import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.IBinder;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Map;

public class MainActivity extends AppCompatActivity {
    //http://www.javaapk.com/tools/flow/6251.html
    //http://www.cnblogs.com/sunzn/archive/2013/01/27/2878400.html
    private TextView txtView;
    private Button button;
    private TrafficService trafficService;
    private DbManager dbManager;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.i("sss", 111 + "");
            trafficService = ((TrafficService.MyBinder) iBinder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.i("sss", 222 + "");
            trafficService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtView = (TextView) findViewById(R.id.textView1);
        button = (Button) findViewById(R.id.button1);
        Intent intent = new Intent(MainActivity.this, TrafficService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        dbManager = new DbManager(this);
        button.setOnClickListener(new View.OnClickListener()

                                  {
                                      @Override
                                      public void onClick(View view) {
                                          if (trafficService == null) {
                                              txtView.setText("服务未绑定");
                                          } else {
                                              trafficService.logRecord();
                                              Map<String, TrafficInfo> map = dbManager.queryTotal();
                                              StringBuilder sb = new StringBuilder();
                                              for (TrafficInfo trafficInfo : map.values()) {
                                                  sb.append(trafficInfo.appName + " - 流量信息:\r\n");
                                                  sb.append("移动网络接收的流量" + Formatter.formatFileSize(MainActivity.this, trafficInfo.mobileRx)).append("\r\n");
                                                  sb.append("移动网络发送的流量" + Formatter.formatFileSize(MainActivity.this, trafficInfo.mobileTx)).append("\r\n");
                                                  sb.append("WIFI接收的流量" + Formatter.formatFileSize(MainActivity.this, trafficInfo.wifiRx)).append("\r\n");
                                                  sb.append("WIFI发送的流量" + Formatter.formatFileSize(MainActivity.this, trafficInfo.wifiTx)).append("\r\n");
                                                  sb.append("--------------------").append("\r\n");
                                                  txtView.setText(sb);
                                              }
                                          }
                                      }
                                  }

        );
    }


    @Override
    protected void onDestroy() {
        unbindService(serviceConnection);
        super.onDestroy();
    }
}
